﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class View3DSystemCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			Document document = cmdData.Application.ActiveUIDocument.Document;
			View3D view3D = null;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Transaction transaction = null;
			Result result;
			try
			{
				transaction = new Transaction(document, "创建三维轴测图");
				transaction.Start();
				Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new PipeFilter(), "选择一根水管");
				Pipe pipe = document.GetElement(reference) as Pipe;
				if (pipe != null)
				{
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
					filteredElementCollector.OfClass(typeof(View3D));
					foreach (Element element in filteredElementCollector)
					{
						if (element.Name == pipe.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM).AsString())
						{
							if (YJKMessageBox.Show("轴测图“" + element.Name + "”已存在，是否覆盖？", MessageBoxButtons.YesNo) == DialogResult.No)
							{
								transaction.RollBack();
							}
							document.DeleteElement(element);
							break;
						}
					}
					XYZ hyendPoint = ((pipe.Location as LocationCurve).Curve as Line).GetEndPoint(0);
					view3D = document.CreatHYView3DSystem(new XYZ(1.0, 1.0, -1.0), hyendPoint);
					view3D.HYSaveOrientationAndLock();
					view3D.HideYJKElements(this.GetAllElementSet(document));
					view3D.UnHideYJKElements(this.GetSystemElementSet(document, pipe));
					view3D.Name = pipe.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM).AsString();
				}
				transaction.Commit();
				activeUIDocument.ActiveView = view3D;
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private ElementSet GetAllElementSet(Document doc)
		{
			ElementSet elementSet = doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(doc).GetFilterByCondition(null, false, null).GetElementIterator();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				elementSet.Insert(element);
			}
			return elementSet;
		}

		private ElementSet GetSystemElementSet(Document doc, Pipe pipe)
		{
			return new HYSystemElementFilter(doc).GetElemSetByParamValue(BuiltInParameter.RBS_SYSTEM_NAME_PARAM, pipe.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM).AsString());
		}
	}
}
